#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System;
using System.Diagnostics;


namespace NUnitGrid.Server.Plugins.Nodes.Node
{
    public class GridNodeMetrics : IGridNodeMetrics
    {
        private readonly Stopwatch taskStopWatch = new Stopwatch();

        public string MachineName { get; private set; }
        public string Nickname { get; private set; }
        public int NumberOfProcessors { get; private set; }
        public int ProcessorMHz { get; private set; }
        public bool Busy { get; set; }
        public int TasksCount { get; set; }

        public TimeSpan CurrentTaskRunTime
        {
            get { return taskStopWatch.Elapsed; }
        }

        public int FailedTasksCount { get; set; }

        public string SoftwareVersion { get; private set; }

        public void OnTaskStarted()
        {
            taskStopWatch.Reset();
            taskStopWatch.Start();
            Busy = true;
        }

        public void OnTaskCompleted()
        {
            taskStopWatch.Stop();
            Busy = false;
        }

        public bool Update(string machineName, string nickName, int numberOfProcessors, int processorMHz,
                           string softwareVersion)
        {
            var changed = (MachineName != machineName || NumberOfProcessors != numberOfProcessors ||
                           ProcessorMHz != processorMHz || Nickname != nickName);
            if (changed)
            {
                MachineName = machineName;
                Nickname = nickName;
                NumberOfProcessors = numberOfProcessors;
                ProcessorMHz = processorMHz;
                SoftwareVersion = softwareVersion;
            }

            return changed;
        }
    }
}